<template>
  <NoActionGoBack/>
  <div class="wrap-wh">
    <div class="ranks-wrap">
      <h1 class="ranks-tit">排行榜</h1>
      <div class="ranks-top">
        <div v-for="item of typeList" :key="item.code" :class="['ranks-top-item',item.active?'top-active':'']"
             @click="tabsChange(item)">
          {{ item.codeValue }}
        </div>
      </div>
      <div class="ranks-content">
        <div class="ranks-content-item" v-for="(item,index) of list" :key="item.id">
          <div class="gold" v-if="index===0"></div>
          <div class="silver" v-else-if="index===1"></div>
          <div class="copper" v-else-if="index===2"></div>
          <div class="num-wrap" v-else>
            <div class="num">{{ index+1 }}</div>
          </div>
          <div class="tel" :style="setStyleColor(index)">{{ filterTel(item.telephone) }}</div>
          <div class="score" :style="setStyleColor(index)">{{ item.score }}分</div>
          <div class="answer-time" :style="setStyleColor(index)">{{ filterTime(item.answer_time) }}</div>
          <span v-if="item.telephone===currentPhone">当前用户</span>
        </div>
        <div v-if="list.length===0" class="no-data">该排行榜暂无数据</div>
      </div>
    </div>
    <div class="go-back">
      <my-button @click="routerGo(currentRoute.query.referer?'question-type':'nav')" text="返回"/>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {reactive,onMounted,ref} from "vue";
import {useRouter, useRoute} from "vue-router";
import _ from 'lodash'

let currentRoute = useRoute()
import axios from '../utils/http'
let currentPhone:any=ref('')
onMounted(()=>{
  currentPhone.value=sessionStorage.getItem('userPhone')
})
function filterTel(val: string) {
  return val.substring(0, 3) + '****' + val.substring(7, 11)
}

function filterTime(val: any) {
  let num = parseFloat(val)
  let min: any = Math.floor(num / 60);
  let sec: any = num % 60;
  if (min < 10) {
    min = '0' + min
  }
  if (sec < 10) {
    sec = '0' + sec
  }
  return `${min}:${sec}`
}

function tabsChange(item: any) {
  for (let item of typeList) {
    item.active = false
  }
  item.active = true
  getList(item.code)

}

function setStyleColor(index: number) {
  let color: string;
  if (index === 0) {
    color = '#F9B94E'
  } else if (index === 1) {
    color = '#B3BCD0'
  } else if (index === 2) {
    color = '#D9813E'
  } else {
    color = '#000'
  }
  return {
    color
  }
}

let list: any[] = reactive([])

let typeList: any[] = reactive([])
initTypeList()

function initTypeList() {
  // @ts-ignore
  let temp = JSON.parse(sessionStorage.getItem('typeList'))
  temp.unshift({
    code: "zongpaihangbang", codeValue: "总排行榜", title: "总排行榜"
  })
//code: "fanzha", codeValue: "反诈", title: "反诈"
  for (let item of temp) {
    item.active = currentRoute.query.id === item.code;
  }
  typeList.push(...temp)
}

getList()

function getList(id: string | number = currentRoute.query.id as string) {
  // for (let i = 0; i < 20; i++) {
  //   list.push({
  //     answer_time: "30",
  //     id: i,
  //     score: 40 + i,
  //     telephone: "19928371723"
  //   })
  // }

  let params: any = {
    bizId: platformConfig.rankingsId,
    pageSize: 99,
    pageNum: 1,
    searchConditions: `qs_category=` + id + ','
  }
  if (id === 'zongpaihangbang') {
    delete params.searchConditions
  }

  axios('/api/appliedModule/page', {
    params
  }).then(({data}) => {
    let temp = data.data.records
    for(let item of temp){
      item.timers=parseFloat(item.answer_time)
    }
    temp = _.orderBy(temp, ['score', 'timers'], ['desc', 'asc'])
    list.length = 0
    list.push(...temp)
  })
}

const router = useRouter()

function routerGo(path: string) {
  router.push({
    path: `/${path}`
  })
}
</script>
<style lang="scss" scoped>
.ranks-wrap {
  margin: 0 auto;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  background: url("../assets/Ranks/rankbg.png") no-repeat;
  width: 1483px;
  height: 883px;
}

.no-data {
  text-align: center;
  padding-top: 300px;
  font-size: 30px;
}

.go-back {
  text-align: center;
  width: 454px;
  margin: 0 auto;
  position: relative;
  top: 40px
}

.ranks-tit {
  text-align: center;
  font-size: 40px;
  font-weight: bold;
  padding-top: 30px;
  padding-bottom: 30px;
}

.ranks-top {
  width: 1160px;
  overflow-x: auto;
  display: flex;
  margin: 0 auto;

  .ranks-top-item {
    border-radius: 15px;
    text-align: center;
    border: 2px solid #3B44DC;
    flex-shrink: 0;
    cursor: pointer;
    margin-right: 20px;
    width: 270px;
    background: #7793F4;
    height: 75px;
    font-size: 30px;
    line-height: 75px;
    font-weight: bold;
    color: #fff;
  }

  .top-active {
    background: #fff;
    color: #000;
    border: 2px solid transparent;
  }
}

.ranks-content {
  width: 1160px;
  height: 650px;
  overflow-y: auto;
  background: #fff;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: #B8CCED 10px 10px 20px;

  .ranks-content-item {
    display: flex;
    //border: 1px solid red;
    box-sizing: border-box;
    height: 66px;
    letter-spacing: 1px;
    line-height: 66px;
    font-size: 30px;
    padding-left: 250px;

    .num-wrap {
      width: 60px;
      height: 60px;
      position: relative;
      top: 2px;
    }

    .num {
      width: 40px;
      height: 40px;
      text-align: center;
      color: #fff;
      font-size: 30px;
      background: #7694F6;
      line-height: 40px;
      position: relative;
      top: 10px;
      left: 10px;
      border-radius: 100%;
    }

    .tel {
      width: 230px;
      margin-left: 20px;
      //  border: 1px solid red;
    }

    .score {
      width: 180px;
      margin-left: 20px; // border: 1px solid red;
    }

    .answer-time {
      width: 180px;
      margin-left: 20px; // border: 1px solid red;
    }

    .gold, .silver, .copper {
      width: 60px;
      height: 66px;
      background-repeat: no-repeat;
    }

    .gold {
      background-image: url("../assets/Ranks/gold.png");
    }

    .silver {
      background-image: url("../assets/Ranks/silver.png");
    }

    .copper {
      background-image: url("../assets/Ranks/copper.png");
    }
  }
}

::-webkit-scrollbar { //滚动条背景
  background-color: transparent;
}

::-webkit-scrollbar-thumb { //滑块内容
  background-color: transparent;
  border-radius: 8px;
}

::-webkit-scrollbar-thumb {
  border: 2px solid silver;
  background-clip: content-box;
}
</style>